<template>
  <a-card :bordered="false" style="margin-left: 10px">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="6" :sm="12">
            <a-form-item label="医生" style="margin-left:8px">
              <a-input placeholder="请搜索医生" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
             <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->


    <!-- table区域-begin -->
    <div>


      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.workImg)" icon="user" />
          </div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">设置</a>
        </span>
      </a-table>
    </div>
    <employee-setup-modal ref="modalForm"></employee-setup-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, postAction, deleteAction, getFileAccessHttpUrl } from '@/api/manage'
import EmployeeSetupModal from './EmployeeSetupModal'

export default {
  name: 'DeptUserInfo',
  mixins: [JeecgListMixin],
  components: {
    EmployeeSetupModal
  },
  data() {
    return {
      description: '医生信息',
      currentDeptId: '',
      currentDept: {},
      queryParam: {
        postCode:"1001"
      },
      // 表头
      columns: [
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'name',
          width: 120
        }, {
          title: '科室',
          align: 'center',
          dataIndex: 'postName',
          width: 120
        }, {
          title: '就诊成本价',
          align: 'center',
          dataIndex: 'registrationFeeCost',
          width: 120,
          customRender: function(t, r, index) {
            return t == 0 ? '~' :'￥'+t;
          },
        },
        {
          title: '就诊销售价',
          align: 'center',
          dataIndex: 'registrationFeeSale',
          customRender: function(t, r, index) {
            return t == 0 ? '~' :'￥'+t;
          },
          width: 100
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 180
        }],
      url: {
        list: '/store/employee/list',
      },
      postOptions: []
    }
  },
  created() {
    const that = this
    getAction(this.url.selectList).then((res) => {
      if (res.success) {
        that.postOptions = res.result
      }
    })
  },

  methods: {
    getAvatarView: function(avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      //if (this.currentDeptId === '') return;
      let params = this.getQueryParams()//查询条件
      params.departId = this.currentDeptId
      getAction(this.url.list, params).then((res) => {
        if (res.success && res.result) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
      })
    },
    batchDel: function() {

      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (!this.currentDeptId) {
        this.$message.error('未选中任何部门，无法取消部门与用户的关联!')
        return
      }

      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        console.log(this.currentDeptId)
        this.$confirm({
          title: '确认取消',
          content: '是否取消用户与选中部门的关联?',
          onOk: function() {
            deleteAction(that.url.deleteBatch, { depId: that.currentDeptId, userIds: ids }).then((res) => {
              if (res.success) {
                that.$message.success('删除用户与选中部门关系成功！')
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    },
    handleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      if (!this.currentDeptId) {
        this.$message.error('未选中任何部门，无法取消部门与用户的关联!')
        return
      }

      var that = this
      deleteAction(that.url.delete, { depId: this.currentDeptId, userId: id }).then((res) => {
        if (res.success) {
          that.$message.success('删除用户与选中部门关系成功！')
          if (this.selectedRowKeys.length > 0) {
            for (let i = 0; i < this.selectedRowKeys.length; i++) {
              if (this.selectedRowKeys[i] == id) {
                this.selectedRowKeys.splice(i, 1)
                break
              }
            }
          }
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    open(record) {
      //console.log(record);
      this.currentDeptId = record.id
      this.currentDept = record
      this.loadData(1)
    },
    clearList() {
      this.currentDeptId = ''
      this.dataSource = []
    },
    hasSelectDept() {
      if (this.currentDeptId == '') {
        this.$message.error('请选择一个科室!')
        return false
      }
      return true
    },
    handleEdit: function(record) {
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.departDisabled = true
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.edit(record)
    },
    handleAdd: function() {
      if (this.currentDeptId == '') {
        this.$message.error('请选择一个科室!')
      } else {
        this.$refs.modalForm.departDisabled = true
        //初始化负责部门
        this.$refs.modalForm.nextDepartOptions = [{ value: this.currentDept.key, label: this.currentDept.title }]
        this.$refs.modalForm.title = '新增'
        this.$refs.modalForm.edit({ activitiSync: '1', userIdentity: 1, selecteddeparts: this.currentDeptId })
      }
    },
    selectOK(data) {
      let params = {}
      params.depId = this.currentDeptId
      params.userIdList = []
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a])
      }
      console.log(params)
      postAction(this.url.edit, params).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleDeptRole(record) {
      if (this.currentDeptId != '') {
        this.$refs.deptRoleUser.add(record, this.currentDeptId)
        this.$refs.deptRoleUser.title = '部门角色分配'
      } else {
        this.$message.warning('请先选择一个部门!')
      }
    },
    handleAccount(record) {
      var param = { userId: record.id, source: '1', mobilePhone: record.mobilePhone, name: record.name }
      this.$refs.modalFormAccount.title = '账号设置'
      this.$refs.modalFormAccount.departDisabled = true
      this.$refs.modalFormAccount.disableSubmit = false
      this.$refs.modalFormAccount.edit(param)
    },
    handleButler() {
      if (this.selectedRowKeys.length == 0) {
        this.$message.warning('请选择员工创建管家身份!')
      } else {
        postAction(this.url.butler, { ids: this.selectedRowKeys }).then((res) => {
          this.$message.success(res.message)
        })
      }
    }
  }
}
</script>
<style scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px
}

.ant-card {
  margin-left: -30px;
  margin-right: -30px;
}

.table-page-search-wrapper {
  margin-top: -16px;
  margin-bottom: 16px;
}
</style>